package stream;

import bean.VideoOrder;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lyy
 * @date 2021/12/26
 */
public class Exercise {
    public static void main(String[] args) {
        // 统计出同时被两个⼈购买的商品列表(交集)
        // 统计出两个⼈购买商品的差集
        // 统计出全部被购买商品的去重并集
        // 统计两个⼈的分别购买订单的平均价格
        // 统计两个⼈的分别购买订单的总价格
        List<VideoOrder> videoOrders1 = Arrays.asList(
                new VideoOrder("20190242812", "springboot教程", 3),
                new VideoOrder("20194350812", "微服务SpringCloud", 5),
                new VideoOrder("20190814232", "Redis教程", 9),
                new VideoOrder("20190523812", "⽹⻚开发教程", 9),
                new VideoOrder("201932324", "百万并发实战Netty", 9));
        List<VideoOrder> videoOrders2 = Arrays.asList(
                new VideoOrder("2019024285312", "springboot教程", 3),
                new VideoOrder("2019081453232", "Redis教程", 9),
                new VideoOrder("20190522338312", "⽹⻚开发教程", 9),
                new VideoOrder("2019435230812", "Jmeter压⼒测试", 5),
                new VideoOrder("2019323542411", "Git+Jenkins持续集成", 7),
                new VideoOrder("2019323542424", "Idea全套教程", 21));
        // 也可以选择重写 equals 和 hashcode 方法
        // 统计出同时被两个⼈购买的商品列表(交集)
        Map<String, List<VideoOrder>> group = Stream.concat(videoOrders1.stream(), videoOrders2.stream()).collect(Collectors.groupingBy(VideoOrder::getTitle));
        String result = group.keySet().stream().filter(key -> group.get(key).size() > 1).collect(Collectors.joining(","));
        System.out.println("交集=" + result);

        // 统计出两个⼈购买商品的差集
        Map<String, List<VideoOrder>> group1 = Stream.concat(videoOrders1.stream(), videoOrders2.stream()).collect(Collectors.groupingBy(VideoOrder::getTitle));
        String result1 = group.keySet().stream().filter(key -> group.get(key).size() <= 1).collect(Collectors.joining(","));
        System.out.println("差集=" + result1);

        // 统计出全部被购买商品的去重并集
        String all = Stream.concat(videoOrders1.stream(), videoOrders2.stream()).map(VideoOrder::getTitle).distinct().collect(Collectors.joining(","));
        System.out.println("去重并集=" + all);

        DoubleSummaryStatistics summaryStatistics = videoOrders1.stream().collect(Collectors.summarizingDouble(VideoOrder::getMoney));
        System.out.println("1 号的平均价格：" + summaryStatistics.getAverage() + "；总价格：" + summaryStatistics.getSum());

        DoubleSummaryStatistics summaryStatistics1 = videoOrders2.stream().collect(Collectors.summarizingDouble(VideoOrder::getMoney));
        System.out.println("2 号的平均价格：" + summaryStatistics1.getAverage() + "；总价格：" + summaryStatistics1.getSum());

    }
}
